<template>
	<view class="page">
		<view class="logo">
			<image src="../../static/logo.png" mode=""></image>
		</view>
		<!-- 填写区 -->
		<view class="input-info">
			<view class="info">
				<input type="tel" v-model="form.phone" maxlength="11" placeholder="手机号">
				<view class="more">

				</view>
			</view>
			<view class="info">
				<input type="tel" v-model="form.code" maxlength="6" placeholder="请输入验证码">
				<view class="more">
					<text class="mo" @click="getCode" :style="isSend?'display:block':'display: none'">获取验证码</text>
					<text class="mo" :style="isSend?'display: none':'display:block'"
						v-model="second">{{second}}秒后重试</text>
				</view>
			</view>
			<view class="info">
				<input :password='!isPassword' maxlength="26" v-model="form.password" placeholder="请输入密码">
				<view class="more">
					<text class="iconfont" :class="isPassword?'icon-eye-on':'icon-eye-off'"
						@click="isPassword = !isPassword"></text>
				</view>
			</view>
		</view>
		<!-- 按钮 -->
		<view class="btn-info">
			<view class="btn" :style="isRegister?'opacity:1':'opacity:0.4'" @click="isRegister?onRegister():''">
				<text>注册</text>
			</view>
		</view>
		<!-- 操作 -->
		<view class="operation">
			<text></text>
			<text @click="onLogin">已有账号?登录</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isPassword: false,
				isRegister: false,
				second: 60,
				isSend: true,
				// 表单
				form: {
					phone: '',
					code: '',
					password: '',
				},
			};
		},
		methods: {
			onLogin() {
				uni.redirectTo({
					url: '/pages/login/login'
				})
			},
			getCode() {
				if (this.form.phone == '' || this.form.phone == '手机号') {
					alert("手机号不能为空！");
					return;
				}
				this.isSend = false;
				this.second = 60;
				console.log(this.phone);
				uni.request({
					data: {
						phone: this.form.phone
					},
					method: 'POST',
					header: {
						"content-type": 'application/x-www-form-urlencoded'
					},
					url: "http://localhost:8089/sendSms",
					success: (res) => {
						if (res.data.success) {
							uni.showToast({
							    title: '发送成功',
							
							    duration: 3000
							});
						} else {
							uni.showToast({
							    title: res.data.message,
								icon: "none",
							    duration: 3000
							});
						}
					}
				});
				var time = 60;
				var _this = this;
				var intervalId = setInterval(function() {
					time = time - 1;
					_this.second = time;
					if (_this.second <= 0) {
						_this.isSend = true
						clearInterval(intervalId);
					}
				}, 1000)
			},
			/**
			 * 注册点击
			 */
			onRegister() {
				uni.request({
					data: {
						phone: this.form.phone,
						password: this.form.password,
						code: this.form.code
					},
					header: {
						"content-type": 'application/x-www-form-urlencoded'
					},
					method: 'POST',
					url: "http://localhost:8089/cgoAdmin/singUp",
					success: (res) => {
						this.$queue.setData("token", res.data.result.token);
						this.$queue.setData("id",  res.data.result.id);
						this.$queue.setData("phone", res.data.result.phone);
						this.$queue.setData("username",  res.data.result.username);
						if (res.data.success) {
							uni.switchTab({
								url: "/pages/home/home"
							})
						}
					}
				});
			}
		},
		watch: {
			form: {
				handler(newValue, oldValue) {
					if (newValue.phone && newValue.code && newValue.password) {
						this.isRegister = true;
					} else {
						this.isRegister = false;
					}
				},
				deep: true
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'register.scss';
</style>
